package com.springboot.mail.modules.log.service.impl;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.springboot.mail.common.ResultUtil;
import com.springboot.mail.common.bean.DeleteDTO;
import com.springboot.mail.common.bean.MyResult;
import com.springboot.mail.common.bean.PageQueryDTO;
import com.springboot.mail.modules.User.domain.entity.UserDO;
import com.springboot.mail.modules.contact.domain.vo.ContactVO;
import com.springboot.mail.modules.log.domain.entity.LogDO;
import com.springboot.mail.modules.log.domain.vo.LogVO;
import com.springboot.mail.modules.log.mapper.LogMapper;
import com.springboot.mail.modules.log.service.LogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class LogServiceImpl extends MPJBaseServiceImpl<LogMapper, LogDO> implements LogService {

    @Resource
    private LogMapper logMapper;

    @Override
    public MyResult<Page<LogVO>> getInfoPageBy(PageQueryDTO pageQueryDTO, LogVO logVO) {

        String userId = this.getLoginUserId();
        System.out.println("userId = " + userId);

        // 连表求总数。s
        MPJLambdaWrapper<LogDO> countWrapper = new MPJLambdaWrapper<LogDO>().eq(LogDO::getUserId, userId);
        Long total = logMapper.selectJoinCount(countWrapper);

        // 连表分页查询。
        MPJLambdaWrapper<LogDO> lambdaWrapper = new MPJLambdaWrapper<LogDO>()
                .selectAll(LogDO.class)
                .selectAs(UserDO::getUsername, LogVO::getUserName)
                .leftJoin(UserDO.class, UserDO::getUserId, LogDO::getUserId)
                .eq(LogDO::getUserId, userId);

        Page<LogVO> logVOIPage = logMapper.selectJoinPage(
                pageQueryDTO.toPage(pageQueryDTO.getCurrent(), pageQueryDTO.getSize(), total, false)
                , LogVO.class, lambdaWrapper);
        System.out.println(logVOIPage.getRecords());//获取分页记录
        System.out.println(logVOIPage.getPages());//总页数
        System.out.println(logVOIPage.getTotal());//总记录数
        System.out.println(logVOIPage.hasPrevious());//是否有上一页
        System.out.println(logVOIPage.hasNext());//是否有下一页
        return ResultUtil.success(logVOIPage);
    }

    @Override
    public MyResult<ContactVO> getInfoByContactId(String contactId) {

        return null;
    }

    @Override
    public MyResult<String> addInfo(ContactVO contactVO) {
        return null;
    }

    @Override
    public MyResult<String> updateInfo(ContactVO contactVO) {
        return null;
    }

    @Override
    public MyResult<String> deleteInfo(String contactId) {
        return null;
    }

    @Override
    public MyResult<String> deleteInfoList(DeleteDTO<String> contactIds) {
        return null;
    }

    /**
     * 获取当前登录的用户ID。
     *
     * @return 当前登录用户的 userId 。
     */
    private String getLoginUserId() {
        return StpUtil.getLoginId().toString().split("_")[1];
    }
}